home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / mpp.exe / MOUSE.CPP < prev    next >
C/C++ Source or Header  |  1992-10-07  |  17KB  |  786 lines

  1. /* -------------------------------------------------------------------- */
  2. /* Mouse++ Version 4.0            mouse.cpp            Revised 10/05/92 */
  3. /*                                                                      */
  4. /* General mouse class for Turbo C++/Borland C++.                       */
  5. /* Copyright 1991, 1992 by Carl W. Moreland                             */
  6. /* -------------------------------------------------------------------- */
  7.  
  8. #include <dos.h>
  9. #include "mouse.h"
  10.  
  11. Mouse mouse;
  12. void interrupt RepeatHandler(...);
  13. void interrupt far (*oldInterrupt_1C)(...);
  14.  
  15. /* -------------------------------------------------------------------- */
  16.  
  17. Mouse::Mouse(void)            // reset mouse - constructor
  18. {
  19.   int found;
  20.   unsigned seg, off;
  21.  
  22.   exists  = 0;                // initialize everything
  23.   enabled = 0;
  24.   visible = 0;
  25.   current.button = 0;
  26.   current.x      = 0;
  27.   current.y      = 0;
  28.   current.xcount = 0;
  29.   current.ycount = 0;
  30.   clickThreshold = 250;
  31.   repeatDelay    = 1000;
  32.   repeatRate     = 200;
  33.   handlerInstalled = 0;
  34.   handlerActive = 0;
  35.   current.event = 0x00;
  36.   buffer.Clear();
  37.  
  38.   _AX = 0x00;                // check for the existance of
  39.   geninterrupt(0x33);            //   a mouse
  40.   found   = _AX;
  41.   buttons = _BX;            // also returns # of buttons
  42.  
  43.   if(found)
  44.     exists = 1;
  45.   if(!exists) return;
  46.  
  47.   _ES = 0;
  48.   _DX = 0;
  49.   _CX = 0;                // clear the event handler
  50.   _AX = 0x14;
  51.   geninterrupt(0x33);
  52.   oldHandler.mask    = _CX;        // save the old event handler
  53.   off = _DX;
  54.   seg = _ES;
  55.   oldHandler.offset  = off;
  56.   oldHandler.segment = seg;
  57.  
  58.   _AX = 0x24;                // get some mouse info
  59.   geninterrupt(0x33);
  60.   Info.majorvers = _BH;
  61.   Info.minorvers = _BL;
  62.   Info.type      = _CH;
  63.   Info.irq       = _CL;
  64.  
  65.   oldInterrupt_1C = getvect(0x1C);
  66.   setvect(0x1C, RepeatHandler);
  67. }
  68.  
  69. Mouse::~Mouse(void)            // restore mouse - destructor
  70. {
  71.   if(!exists) return;
  72.  
  73.   _AX = 0x00;                // reset mouse
  74.   geninterrupt(0x33);
  75.  
  76.   if(oldHandler.mask)            // restore old event handler
  77.     InstallHandler(oldHandler);
  78.  
  79.   setvect(0x1C, oldInterrupt_1C);
  80. }
  81.  
  82. void Mouse::Enable(void)        // enable the mouse
  83. {
  84.   if(!exists || enabled) return;
  85.  
  86.   if(currentHandler.mask && !handlerInstalled)
  87.     InstallHandler();            // re-install event handler
  88.  
  89.   enabled = 1;
  90. }
  91.  
  92. void Mouse::Disable(void)        // disable the mouse
  93. {
  94.   if(!exists || !enabled) return;
  95.  
  96.   if(handlerInstalled)
  97.   {
  98.     ClearHandler();            // disable the event handler
  99.     ClearClick();            // clear the multi-click buffers
  100.   }
  101.   Hide();                // turn the cursor off
  102.   enabled = 0;
  103. }
  104.  
  105. void Mouse::Show(void)                // show mouse cursor
  106. {
  107.   if(!exists || !enabled) return;
  108.   if(visible) return;
  109.  
  110.   if(cgcActive)
  111.     cgc->Show();
  112.   else
  113.   {
  114.     _AX = 0x01;
  115.     geninterrupt(0x33);
  116.   }
  117.   visible = 1;
  118. }
  119.  
  120. void Mouse::Hide(void)            // hide mouse cursor
  121. {
  122.   if(!exists || !enabled) return;
  123.   if(visible < 1) return;
  124.  
  125.   if(cgcActive)
  126.     cgc->Hide();
  127.   else
  128.   {
  129.     _AX = 0x02;
  130.     geninterrupt(0x33);
  131.   }
  132.   visible = 0;
  133. }
  134.  
  135. void Mouse::Move(int x, int y)        // move cursor to col, row
  136. {                    // col & row are pixel coordinates
  137.   if(!exists || !enabled) return;
  138.  
  139.   _DX = y;
  140.   _CX = x;
  141.   _AX = 0x04;
  142.   geninterrupt(0x33);
  143. }
  144.  
  145. void Mouse::xLimit(int min, int max)    // set min/max column range
  146. {                    // min & max are pixel coordinates
  147.   if(!exists) return;
  148.  
  149.   _DX = max;
  150.   _CX = min;
  151.   _AX = 0x07;
  152.   geninterrupt(0x33);
  153. }
  154.  
  155. void Mouse::yLimit(int min, int max)    // set min/max row range
  156. {                    // min & max are pixel coordinates
  157.   if(!exists) return;
  158.  
  159.   _DX = max;
  160.   _CX = min;
  161.   _AX = 0x08;
  162.   geninterrupt(0x33);
  163. }
  164.  
  165. void Mouse::xyLimit(int xmin, int xmax, int ymin, int ymax)
  166. {
  167.   xLimit(xmin, xmax);
  168.   yLimit(ymin, ymax);
  169. }
  170.  
  171. int Mouse::GetVideoPage(void)        // get the cursor's display page
  172. {
  173.   if(!exists) return 0;
  174.  
  175.   _AX = 0x1E;
  176.   geninterrupt(0x33);
  177.   return _BX;
  178. }
  179.  
  180. void Mouse::SetVideoPage(int page)    // set the cursor's display page
  181. {
  182.   if(!exists) return;
  183.  
  184.   _BX = page;
  185.   _AX = 0x1D;
  186.   geninterrupt(0x33);
  187. }
  188.  
  189. void Mouse::MickToPix(int horiz, int vert)
  190. {                             // set the mickey to pixel ratio
  191.   if(!exists) return;
  192.  
  193.   _DX = vert;
  194.   _CX = horiz;
  195.   _AX = 0x0F;
  196.   geninterrupt(0x33);
  197. }
  198.  
  199. void Mouse::SetSpeedThreshold(unsigned speed)
  200. {                    // set speed change threshold
  201.   if(!exists) return;
  202.  
  203.   _DX = speed;
  204.   _AX = 0x13;
  205.   geninterrupt(0x33);
  206. }
  207.  
  208. void Mouse::SetCursor(TextCursor& cursor)
  209. {                    // change the text cursor
  210.   int wasVisible = 0;
  211.  
  212.   if(!exists) return;
  213.   if(visible)
  214.   {
  215.     wasVisible = 1;
  216.     Hide();
  217.   }
  218.  
  219.   _DX = cursor.cMask;
  220.   _CX = cursor.sMask;
  221.   _BX = cursor.type;
  222.   _AX = 0x0A;
  223.   geninterrupt(0x33);
  224.   cgcActive = 0;
  225.  
  226.   if(wasVisible)
  227.     Show();
  228. }
  229.  
  230. void Mouse::SetCursor(GraphicsCursor& cursor)
  231. {                    // change the graphics cursor
  232.   int wasVisible = 0;
  233.  
  234.   if(!exists) return;
  235.   if(visible)
  236.   {
  237.     wasVisible = 1;
  238.     Hide();
  239.   }
  240.  
  241.   if(cursor.type == 0)            // std 16x16 cursor
  242.   {
  243.     _ES = FP_SEG(cursor.cImage);
  244.     _DX = FP_OFF(cursor.cImage);
  245.     _CX = cursor.hoty;
  246.     _BX = cursor.hotx;
  247.     _AX = 0x09;
  248.     geninterrupt(0x33);
  249.   }
  250.   else                    // arbitrary cursor size
  251.   {
  252.     _ES = FP_SEG(cursor.cImage);
  253.     _DX = FP_OFF(cursor.cImage);
  254.     _CH = cursor.cHeight;
  255.     _CL = cursor.hoty;
  256.     _BH = cursor.cWords;
  257.     _BL = cursor.hotx;
  258.     _AX = 0x12;
  259.     geninterrupt(0x33);
  260.   }
  261.  
  262.   gc   = &cursor;
  263.   cgcActive = 0;
  264.  
  265.   if(handlerInstalled)            // reinstall the current handler
  266.     InstallHandler();
  267.  
  268.   if(wasVisible)
  269.     Show();
  270. }
  271.  
  272. void Mouse::SetCursor(ColorGraphicsCursor& cursor)
  273. {
  274.   int wasVisible = 0;
  275.  
  276.   if(!exists) return;
  277.   if(visible)
  278.   {
  279.     wasVisible = 1;
  280.     Hide();
  281.   }
  282.  
  283.   cgc  = &cursor;
  284.   cgcActive = 1;
  285.  
  286.   if(handlerInstalled)            // reinstall the current handler
  287.     InstallHandler();
  288.  
  289.   if(wasVisible)            // need to get the current coords
  290.   {
  291.     _AX = 0x03;
  292.     geninterrupt(0x33);
  293.     cursor.x = _CX;
  294.     cursor.y = _DX;
  295.     Show();
  296.   }
  297. }
  298.  
  299. void Mouse::Position(void)        // update cursor position &
  300. {                    // button status
  301.   if(!exists || !enabled) return;
  302.  
  303.   _AX = 0x03;
  304.   geninterrupt(0x33);
  305.   current.button = _BX;
  306.   current.x = _CX;
  307.   current.y = _DX;
  308. }
  309.  
  310. int Mouse::Pressed(int button)        // check press status of button
  311. {
  312.   if(!exists || !enabled) return(0);
  313.  
  314.   if(handlerInstalled)
  315.   {
  316.     if(button == LEFTBUTTON)
  317.       return(current.event & LB_PRESSED);
  318.     else if(button == RIGHTBUTTON)
  319.       return(current.event & RB_PRESSED);
  320.     else if(button == CENTERBUTTON)
  321.       return(current.event & CB_PRESSED);
  322.   }
  323.  
  324.   _BX = button;
  325.   _AX = 0x05;
  326.   geninterrupt(0x33);
  327.   current.button = _AX;
  328.   int BX = _BX;
  329.   if(BX)
  330.   {
  331.     current.x = _CX;
  332.     current.y = _DX;
  333.   }
  334.   return(BX);
  335. }
  336.  
  337. int Mouse::Released(int button)        // check release status of button
  338. {
  339.   if(!exists || !enabled) return(0);
  340.  
  341.   if(handlerInstalled)
  342.   {
  343.     if(button == LEFTBUTTON)
  344.       return(current.event & LB_RELEASED);
  345.     else if(button == RIGHTBUTTON)
  346.       return(current.event & RB_RELEASED);
  347.     else if(button == CENTERBUTTON)
  348.       return(current.event & CB_RELEASED);
  349.   }
  350.  
  351.   _AX = 0x06;
  352.   _BX = button;
  353.   geninterrupt(0x33);
  354.   current.button = _AX;
  355.   int BX = _BX;
  356.   if(BX)
  357.   {
  358.     current.x = _CX;
  359.     current.y = _DX;
  360.   }
  361.   return(BX);
  362. }
  363.  
  364. void Mouse::Motion(void)        // get # of mickeys moved
  365. {
  366.   if(!exists || !enabled) return;
  367.  
  368.   _AX = 0x0B;
  369.   geninterrupt(0x33);
  370.   current.xcount = _CX;
  371.   current.ycount = _DX;
  372. }
  373.  
  374. int Mouse::InBox(int left,  int top,    // see if mouse is in a box
  375.                  int right, int bottom)
  376. {
  377.   if(!exists || !enabled) return(0);
  378.  
  379.   if((current.x >= left)  && (current.y >= top) &&
  380.      (current.x <= right) && (current.y <= bottom))
  381.     return(1);
  382.   return(0);
  383. }
  384.  
  385. void Mouse::Exclude(int left,  int top,    // set up exclusion area
  386.                     int right, int bottom)
  387. {
  388.   static unsigned char hotx, hoty, height, width;
  389.  
  390.   if(!exists || !enabled) return;
  391.  
  392.   if(cgcActive)
  393.   {
  394.     hotx   = cgc->hotx;
  395.     hoty   = cgc->hoty;
  396.     height = cgc-